<?php
/**
 * @name 销售单控制器
 * @Author : yu
 * @Created : 2016-11-1
 * @Copyright : MPE.Fiidee.Monda.Cn
 */
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;


class SaleAction extends CommonAction {

	public function C_start()
	{
		parent::C_start();	
	}


	protected $serviceBean="trading.tradingSale.service";

	public function index(HttpRequest $request)
	{
		$saleService = Beans::get($this->serviceBean);
		$orderService = Beans::get('trading.tradingOrder.service');

		$page = $request->getParameter('page');
		$pagesize = $request->getParameter('pagesize');

		if ($page <= 0) $page = 1;
		if ($pagesize <= 0) $pagesize = 15;
		$this->page=$page;
		$this->pagesize=$pagesize;

		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),
			"select_mix_option2" => $request->getParameter('select_mix_option2'),
			"start_time" => $request->getParameter('start_time'),
			"end_time" => $request->getParameter('end_time'),
		);

        if( $request -> getParameter('export')==1 ){
            $page =1;
            $pagesize =2147483648;
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $tmp = "('0'";
                foreach ($ids as $value){
                    $tmp .= ','."'$value'";
                }
                $search_arr['id']=$tmp.')';
            }
        }

		$arr = $saleService->search($pagesize, $page, $search_arr);

		$items = $arr['items'];

		$itemsLen = count($items);

		for ($i = 0; $i < $itemsLen; $i++) {

			$items[$i]['shipping_way'] = $orderService->getShippingwayText($items[$i]['shipping_way']);
			$items[$i]['pay_way'] = $orderService->getPaywayText2($items[$i]['pay_way']);
		}

		//var_dump($items);
        if( $request->getParameter('export') == 1){
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["sale_no"];
                $tmp[] = $v["actual_amount"];
                $tmp[] = $v["username"];
                $tmp[] = $v["order_no"];
                $tmp[] = $v["invoice_no"];
                $tmp[] = $v["contract_no"];
                $tmp[] = $v["order_time"];
                $tmp[] = $v["pay_time"];
                $tmp[] = $v["delivery_time"];
                $tmp[] = $v["pay_way"];
                $tmp[] = $v["shipping_way"];
                $excel[] = $tmp;
            }
            $header = array('销售单号', '销售金额(￥)', '买家会员号', '订单号', '发货单号', '合同号', '下单时间','付款时间','发货时间','支付方式','配送方式');
            phpexcel("销售列表",$header,$excel);
            exit();
        }

		$this->generatePageMenu($arr['total']);

		$this->assign('items', $items);
		$this->assign('search_arr', $search_arr);

		$this->assign("title", "销售单列表");
		$this->assign('bread', array('报表管理',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));

		$this->setView(strtolower($request->getAction()) . '/index');
	}
	public function ranking(HttpRequest $request)
	{
		$saleService = Beans::get($this->serviceBean);

		$page = $request->getParameter('page');
		$pagesize = $request->getParameter('pagesize');

		if ($page <= 0) $page = 1;
		if ($pagesize <= 0) $pagesize = 15;
		$this->page=$page;
		$this->pagesize=$pagesize;

		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),
			"start_time" => $request->getParameter('start_time'),
			"end_time" => $request->getParameter('end_time'),
		);

        //导出excel add By Laoliu
        if( $request -> getParameter('export')==1 ){
            $pagesize =99999;
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $tmp = "('0'";
                foreach ($ids as $value){
                    $tmp .= ','."'$value'";
                }
                $search_arr['ids']=$tmp.')';
            }
        }

		$arr = $saleService->searchByRanking($pagesize, $page, $search_arr);

		$items = $arr['items'];

		$itemsLen = count($items);

		for ($i = 0; $i < $itemsLen; $i++) {

			$items[$i]['total_sale_profit'] = bcsub($items[$i]['total_sale_amount'],$items[$i]['total_cost_price'],2);
		}

        //导出excel add By Laoliu
        if( $request->getParameter('export') == 1){
            $excel = array();
            $order = 0;
            foreach($items as $k => $v)
            {
                $order +=1;
                $tmp = array();
                $tmp[] = $order;
                $tmp[] = $v["product_no"];
                $tmp[] = $v["type_name"];
                $tmp[] = $v["product_name"];
                $tmp[] = $v["factory_name"];
                $tmp[] = $v["brand_name"];
                $tmp[] = $v["pkg_size"];
                $tmp[] = $v["total_sale_quantity"];
                $tmp[] = $v["total_sale_amount"];
                $tmp[] = $v["total_sale_profit"];
                $excel[] = $tmp;
            }
            $header = array('排行', '货号', '类型', '品名', '厂家', '牌号', '包装规格(KG/袋)','销售量(t)','销售额(￥)','利润(￥)');
            phpexcel("销售列表",$header,$excel);
            exit();
        }

		$this->generatePageMenu($arr['total']);

		$this->assign('items', $items);
		$this->assign('search_arr', $search_arr);
		$this->assign('pagesize', $pagesize);

		$this->assign("title", "销售排行");
		$this->assign('bread', array('报表管理',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/ranking');
	}

	public function details(HttpRequest $request)
	{
		$saleService = Beans::get($this->serviceBean);
		$orderService = Beans::get('trading.tradingOrder.service');

		$page = $request->getParameter('page');
		$pagesize = $request->getParameter('pagesize');

		if ($page <= 0) $page = 1;
		if ($pagesize <= 0) $pagesize = 15;
		$this->page=$page;
		$this->pagesize=$pagesize;


		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),
			"start_time" => $request->getParameter('start_time'),
			"end_time" => $request->getParameter('end_time'),

		);

        //导出excel add By Laoliu
        if( $request -> getParameter('export')==1 ){
            $pagesize =99999;
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $tmp = "('0'";
                foreach ($ids as $value){
                    $tmp .= ','."'$value'";
                }
                $search_arr['ids']=$tmp.')';
            }
        }

		$arr = $saleService->searchByDetails($pagesize, $page, $search_arr);

		$items = $arr['items'];

		$itemsLen = count($items);

		for ($i = 0; $i < $itemsLen; $i++) {
            if($items[$i]['cost_price']==0){//成本价为空，不计算利润

				$items[$i]['sale_profit'] ='--';
			}else{

				$items[$i]['sale_profit'] = bcsub($items[$i]['sale_amount'],($items[$i]['cost_price']*$items[$i]['sale_quantity']),2);
			}
			$items[$i]['pay_way'] = $orderService->getPaywayText($items[$i]['pay_way']);
		}

        //导出excel add By Laoliu
        if( $request->getParameter('export') == 1){
            $excel = array();
            foreach($items as $k => $v)
            {
                $order +=1;
                $tmp = array();
                $tmp[] = $v["order_no"];
                $tmp[] = $v["pay_way"];
                $tmp[] = $v["product_no"];
                $tmp[] = $v["type_name"];
                $tmp[] = $v["product_name"];
                $tmp[] = $v["factory_name"];
                $tmp[] = $v["brand_name"];
                $tmp[] = $v["pkg_size"];
                $tmp[] = $v["sale_quantity"];
                $tmp[] = $v["sale_amount"];
                $tmp[] = $v["sale_profit"];
                $tmp[] = $v["order_time"];
                $excel[] = $tmp;
            }
            $header = array('订单号', '支付方式', '货号', '类型', '品名', '厂家', '牌号','包装规格(KG/袋)','销售量(t)','销售额(￥)','利润(￥)','销售时间');
            phpexcel("销售明细",$header,$excel);
            exit();
        }

		$this->generatePageMenu($arr['total']);

		$this->assign('items', $items);
		$this->assign('search_arr', $search_arr);
		$this->assign('pagesize', $pagesize);

		$this->assign("title", "销售明细");
		$this->assign('bread', array('报表管理',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/details');
	}

    public function test() {

        $service = Beans::get('api.mpe.service');

        $service->cloneAll();
        die;

   }
}
